package Assesment
{
	import Assets.Controls.containerStyle;
	
	import SplineComponent.SplineComponentView;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.media.Camera;
	import flash.media.Video;
	
	import mvc.Controller;
	
	import mx.containers.Panel;
	import mx.containers.TitleWindow;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.RadioButton;
	import mx.controls.RadioButtonGroup;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import mx.effects.WipeUp;
	import mx.effects.Zoom;
	
	import util.Observable;
	//import mx.graphics.
	
	public class AssesmentView extends SplineComponentView
	{
		private var container_obj:containerStyle=new containerStyle;
		private var mainPanel:TitleWindow;
		public var QuesPanel:Panel;
		private var AddQuesTypePanel:Panel;
		private var OkButton:Button;
		private var quesType:RadioButtonGroup;
		private var multipleOrDescriptive:RadioButtonGroup;
		private var multipleChoiceType:RadioButton;
		private var descriptiveType:RadioButton;
		private var QuestionType:String;
		private var videoType:RadioButton;
		private var audioType:RadioButton;
		private var textType:RadioButton;
		private var initialHeight:Number=300;
		private var initialWidth:Number=400;
		private var initialX:Number=200;
		private var initialY:Number=200;
		private var nextQuesButton:Button;
		private var saveQuestionButton:Button;
		public var askForAnswerText:TextInput;
		public var answerStatement:TextArea;
		public var askForWeightage:TextInput;
		public var mystage:Object;
		
		private var zoom:Zoom;
		public var video:Video;
		private var controller:Object;
		public var recordButton:Button;
		public var videoHolder:UIComponent;
		public var audioRecordButton:Button;
		private var wipeup:WipeUp;
		private var questionNumber:Number=1;
		public var AOption:TextInput;
		public var BOption:TextInput;
		public var COption:TextInput;
		public var DOption:TextInput;
		public var questionStatement:TextArea;
		
		private var isAudioTypeSelected:Boolean=false;
		private var isVideoTypeSelected:Boolean=false;
		private var isMultipleChoiceTypeSelected:Boolean=false;
		private var isDescriptiveTypeSelected:Boolean=false;
		private var previousPanel:Panel=new Panel;
		private var previousRadioButton=new RadioButton;
		private var lessonEditMode_bool:Boolean;
		public var assesmentPanel:TitleWindow;
		private var previousQuestionButton:Button;
		private var initialWidthOfResizablePanel:Number;
		private var initialHeightOfResizablePanel:Number;
		//[Bindable]
		//[Embed(source="Assets/next.jpg")]
		//public var nextIcon:Class;
		
		//[Embed(source="Assets/previous.jpg")]
		//public var previousIcon:Class;
		
		public function AssesmentView(model:Observable,controller:Controller,lessonView:Object, lessonEditMode:Boolean):void
		{
			super(model,controller);
			this.controller=this.getController();
			this.mystage=lessonView;
			this.lessonEditMode_bool = lessonEditMode;
			initializeAssesmentPanel();
			super.makePanelDraggable(this.assesmentPanel);
			super.makePanelResizable();
		}
		
	//----------------------------------------function for resizing--------------------------------------------//
		override public function setNewCordinates(infoObj:Object):void
		{
			this.assesmentPanel.x=infoObj.finalPanelX;
			this.assesmentPanel.y=infoObj.finalPanelY;
			this.assesmentPanel.width=infoObj.finalPanelWidth;
			this.assesmentPanel.height=infoObj.finalPanelHeight;
			this.initialWidthOfResizablePanel=infoObj.initialWidth;
			this.initialHeightOfResizablePanel=infoObj.intialHeight;
			
			setCordinatesForAdditionalPanels();
		}
		
		public function setCordinatesForAdditionalPanels():void
		{
			var ratioX:Number = (this.assesmentPanel.width - initialWidthOfResizablePanel)/initialWidthOfResizablePanel;
			var ratioY:Number = (this.assesmentPanel.height - initialHeightOfResizablePanel)/initialHeightOfResizablePanel;
			
			resize(ratioX,ratioY,this.QuesPanel);
			
		}
		
		private function resize(ratioX:Number,ratioY:Number,panelResized:Panel):void
		{
			if(panelResized==null)
			{
				return;
			}
			var children: Array = panelResized.getChildren();
				
				for(var i:int = 0; i < children.length; i++)
				{
					var obj:DisplayObject;
					
					obj = DisplayObject(children[i]);
					
					obj.x += obj.x * ratioX;
					obj.width += obj.width * ratioX;
					obj.y += obj.y * ratioY;
					obj.height += obj.height * ratioY;
					
				}
		}
	
	//--------------------------------------------------------------------------------------------------------//	
		public function initializeAssesmentPanel():void
		{
			assesmentPanel=new TitleWindow;
			assesmentPanel=container_obj.setPanel(300,100,500,500,true,1,"FRAME QUESTIONS","absolute",10,10,30,20);
			this.mystage.addChildToStage(assesmentPanel);
			
			quesType= new RadioButtonGroup();
			
			videoType=new RadioButton();
			videoType.x=5;
			videoType.y=5;
			videoType.label="Video";
			videoType.groupName="quesType";
		    videoType.addEventListener(MouseEvent.CLICK,evtListner_addVideoQuestionPanel);
		    
		    audioType=new RadioButton();
			audioType.label="Audio";
			audioType.x=5;
			audioType.y=25;
			audioType.groupName="quesType";
			audioType.addEventListener(MouseEvent.CLICK,evtListner_addAudioQuestionPanel);
			
			textType=new RadioButton();
			textType.label="Text";
			textType.x=5;
			textType.y=45;
			textType.groupName="quesType";
			textType.addEventListener(MouseEvent.CLICK,evtListner_multipleChoiceOrDescriptive);
			
			/*----this group is visible to panel when textType is selected-----*/
			
			
			multipleOrDescriptive= new RadioButtonGroup();
			
			multipleChoiceType=new RadioButton();
			multipleChoiceType.x=15;
			multipleChoiceType.y=60;
			multipleChoiceType.label="MULTIPLE CHOICE";
			multipleChoiceType.groupName="multipleOrDescriptive";
			multipleChoiceType.visible=false;
			multipleChoiceType.addEventListener(MouseEvent.CLICK,evtListner_addMultipleChoiceQuestionPanel);
			assesmentPanel.addChild(multipleChoiceType);
			
			descriptiveType=new RadioButton;
			descriptiveType.x=15;
			descriptiveType.y=75;
			descriptiveType.label="DESCRIPTIVE";
			descriptiveType.groupName="multipleOrDescriptive";
			descriptiveType.visible=false;
			descriptiveType.addEventListener(MouseEvent.CLICK,evtListner_addDescriptiveQuestionPanel);
			assesmentPanel.addChild(descriptiveType);
			
			assesmentPanel.addChild(videoType);
			assesmentPanel.addChild(audioType);
			assesmentPanel.addChild(textType);
			
			
		}
		
		
		
		public function evtListner_addVideoQuestionPanel(evt:MouseEvent):void
		{	
			isVideoTypeSelected=true;
			
			if(multipleChoiceType.visible==true)
			{
				multipleChoiceType.visible=false;
				descriptiveType.visible=false;
			}
			
			if(isAudioTypeSelected==true||isMultipleChoiceTypeSelected==true||isDescriptiveTypeSelected==true)
			{
				assesmentPanel.removeChild(previousPanel);
			}
			
			
			QuesPanel=new TitleWindow;
			QuesPanel=container_obj.setPanel(0,100,assesmentPanel.width-20,assesmentPanel.height-155,true,1,"Question No."+questionNumber,"absolute",0,0,20,0);
			assesmentPanel.addChild(QuesPanel);
			
			
			zoomInFunction(QuesPanel);
			
			videoHolder = new UIComponent();
		   		
		   	var camera:Camera = Camera.getCamera();
		  	video = new Video(camera.width*2, camera.height*2);
			camera.setMode(camera.width,camera.height,75,true);
		  	
		   	video.attachCamera(camera);
		   	videoHolder.addChild(video);
		   	QuesPanel.addChild(videoHolder);
		   	videoHolder.x=5;
		   	videoHolder.y =10;
		   		
		   	//add record Button
		  	recordButton=new Button;
		   	recordButton=container_obj.setButton(5,300,100,20,true,"RECORD",null,"Record",0);
		   	QuesPanel.addChild(recordButton);
		   	recordButton.addEventListener(MouseEvent.CLICK,evtListner_recordVideo);
		   			
        	audioRecordButton=new Button;
        	audioRecordButton=container_obj.setButton(120,300,100,20,true,"Audio",null,"Audio",0);
        	QuesPanel.addChild(audioRecordButton);
        	audioRecordButton.addEventListener(MouseEvent.CLICK,evtListner_recordAudio);
		
			isAudioTypeSelected=false;
			isMultipleChoiceTypeSelected==false;
			isDescriptiveTypeSelected==false;
			previousPanel=QuesPanel;	
			previousRadioButton=videoType;
		}
		
		public function evtListner_addAudioQuestionPanel(evt:MouseEvent):void
		{
			isAudioTypeSelected=true;
			
			if(multipleChoiceType.visible==true)
			{
				multipleChoiceType.visible=false;
				descriptiveType.visible=false;
			}
			
			if(isVideoTypeSelected==true||isMultipleChoiceTypeSelected==true||isDescriptiveTypeSelected==true)
			{
				assesmentPanel.removeChild(previousPanel);
			}
			
			QuesPanel=new TitleWindow;
			QuesPanel=container_obj.setPanel(0,100,assesmentPanel.width-20,assesmentPanel.height-155,true,1,"Question No."+questionNumber,"absolute",0,0,20,0);
			assesmentPanel.addChild(QuesPanel);
			
			zoomInFunction(QuesPanel);
			
			isVideoTypeSelected=false;
			isMultipleChoiceTypeSelected==false;
			isDescriptiveTypeSelected==false;
			previousPanel=QuesPanel;
			previousRadioButton=audioType;
		}
		
		public function evtListner_multipleChoiceOrDescriptive(evt:MouseEvent):void
		{
			multipleChoiceType.visible=true;
			descriptiveType.visible=true;
			
		}
		
		public function evtListner_addMultipleChoiceQuestionPanel(evt:MouseEvent):void
		{
			isMultipleChoiceTypeSelected=true;
			
			if(isVideoTypeSelected==true||isAudioTypeSelected==true||isDescriptiveTypeSelected==true)
			{
				assesmentPanel.removeChild(previousPanel);
			}
			
			QuesPanel=new TitleWindow;
			QuesPanel=container_obj.setPanel(0,100,assesmentPanel.width-20,assesmentPanel.height-155,true,1,"Question No."+questionNumber,"absolute",0,0,20,0);
			assesmentPanel.addChild(QuesPanel);
			
			zoomInFunction(QuesPanel);
			
				questionStatement= new TextArea();
        		questionStatement.x=10;
       			questionStatement.y=25;
        		questionStatement.width=460;
       			questionStatement.height=70;
       			
        		var ALabel:Label=new Label;
        		ALabel.text="A)";
        		ALabel.x=20;
        		ALabel.y=110;
        		
        		var BLabel:Label=new Label;
        		BLabel.text="B)";
        		BLabel.x=20;
        		BLabel.y=135;
        		
        		var CLabel:Label=new Label;
        		CLabel.text="C)";
        		CLabel.x=20;
        		CLabel.y=160;
        		
        		var DLabel:Label=new Label;
       		 	DLabel.text="D)";
       			DLabel.x=20;
        		DLabel.y=185;
        		
        		AOption=new TextInput();
       			AOption.x=100;
        		AOption.y=110;
        		AOption.width=200;
        		
       			BOption=new TextInput();
        		BOption.x=100;
        		BOption.y=135;
        		BOption.width=200;
        		
       		    COption=new TextInput();
        		COption.x=100;
        		COption.y=160;
        		COption.width=200;
        		
        		DOption=new TextInput();
        		DOption.x=100;
        		DOption.y=185;
        		DOption.width=200;
        		
        		var AnswerLabel:Label=new Label;
        		AnswerLabel.text="ANSWER";
        		AnswerLabel.x=20;
        		AnswerLabel.y=210;
        		AnswerLabel.width=75;
        		QuesPanel.addChild(AnswerLabel);
        		
        		var WeightageLabel:Label=new Label;
        		WeightageLabel.text="WEIGHTAGE";
        		WeightageLabel.x=20;
        		WeightageLabel.y=235;       		
        		WeightageLabel.width=75;
        		QuesPanel.addChild(WeightageLabel);
        		
        		askForAnswerText=new TextInput;
        		askForAnswerText=container_obj.setTextInput(100,210,200,20,true,"ANSWER");
        		QuesPanel.addChild(askForAnswerText);
        	
        		askForWeightage=new TextInput;
        		askForWeightage=container_obj.setTextInput(100,235,200,20,true,"WEIGHTAGE");
        		QuesPanel.addChild(askForWeightage);
        		
        		isVideoTypeSelected=false;
				isAudioTypeSelected==false;
				isDescriptiveTypeSelected==false;
				previousPanel=QuesPanel;
				previousRadioButton=multipleChoiceType;
				
				
        	    nextQuesButton =new Button;
        		nextQuesButton=container_obj.setButton(260,290,70,20,true,"next",null,"NEXT",0);
        		QuesPanel.addChild(nextQuesButton);
        		nextQuesButton.addEventListener(MouseEvent.CLICK,evtListner_showNextAndWriteToXml);
        		
        		previousQuestionButton=new Button;
        		previousQuestionButton=container_obj.setButton(70,290,70,20,true,"PREVIOUS",null,"PREVIOUS",0);
        		QuesPanel.addChild(previousQuestionButton);
        		previousQuestionButton.addEventListener(MouseEvent.CLICK,evtListner_showPreviousQuestion);
        		
        		saveQuestionButton=new Button;
        		saveQuestionButton=container_obj.setButton(170,290,70,20,true,"SAVE",null,"SAVE",0);
        		QuesPanel.addChild(saveQuestionButton);
        		saveQuestionButton.addEventListener(MouseEvent.CLICK,evtListner_WriteToArray);
        		
        		QuesPanel.addChild(AOption);
       		 	QuesPanel.addChild(BOption);
        		QuesPanel.addChild(COption);
        		QuesPanel.addChild(DOption);   
        		QuesPanel.addChild(questionStatement);
        		QuesPanel.addChild(ALabel);
        		QuesPanel.addChild(BLabel);
        		QuesPanel.addChild(CLabel);
        		QuesPanel.addChild(DLabel);
        		
        		
		}
		
		public function evtListner_addDescriptiveQuestionPanel(evt:MouseEvent):void
		{
			isDescriptiveTypeSelected=true;
			
			if(isVideoTypeSelected==true||isMultipleChoiceTypeSelected==true||isAudioTypeSelected==true)
			{
				assesmentPanel.removeChild(previousPanel);
			}
			
			QuesPanel=new TitleWindow;
			QuesPanel=container_obj.setPanel(0,100,assesmentPanel.width-20,assesmentPanel.height-155,true,1,"Question No."+questionNumber,"absolute",0,0,20,0);
			assesmentPanel.addChild(QuesPanel);
			
			zoomInFunction(QuesPanel);
			
				questionStatement= new TextArea();
        		questionStatement.x=20;
       			questionStatement.y=20;
        		questionStatement.width=280;
       			questionStatement.height=70;
       			QuesPanel.addChild(questionStatement);
       			
       			answerStatement= new TextArea();
        		answerStatement.x=20;
       			answerStatement.y=100;
        		answerStatement.width=280;
       			answerStatement.height=150;
       			QuesPanel.addChild(answerStatement);
       			
      			isVideoTypeSelected=false;
				isMultipleChoiceTypeSelected==false;
				isAudioTypeSelected==false;
				previousPanel=QuesPanel;
				previousRadioButton=descriptiveType;
				
				saveQuestionButton=new Button;
        		saveQuestionButton=container_obj.setButton(170,290,70,20,true,"SAVE",null,"SAVE",0);
        		QuesPanel.addChild(saveQuestionButton);
        		//saveQuestionButton.addEventListener(MouseEvent.CLICK,evtListner_Save);
        		
				nextQuesButton =new Button;
        		nextQuesButton=container_obj.setButton(260,290,70,20,true,"next",null,"NEXT",0);
        		QuesPanel.addChild(nextQuesButton);
        	//	nextQuesButton.addEventListener(MouseEvent.CLICK,evtListner_showNext);
			
		}
		        	
        	public function evtListner_recordAudio(evt:MouseEvent):void
        	{
        		controller.recordAudio();
        	}
        	
        	
        	public function evtListner_recordVideo(evt:MouseEvent):void
        	{
        		controller.recordVideo();
        	}
        	
        	public function zoomInFunction(myZoomingPanel:Object)
        	{
        		zoom=new Zoom;
  				zoom.zoomHeightFrom=0;
       			zoom.zoomWidthFrom=0;
       			zoom.zoomHeightTo=1;
       			zoom.zoomWidthTo=1;
       			zoom.target=myZoomingPanel;
       			zoom.duration=3000;
       			zoom.play();      	
        	}
        
        	
        	public function zoomOutFunction(myZoomingPanel:Object)
        	{
        		zoom.zoomHeightFrom=1;
       			zoom.zoomWidthFrom=1;
       			zoom.zoomHeightTo=0;
       			zoom.zoomWidthTo=0;
       			zoom.target=myZoomingPanel;
       			zoom.duration=1000;
       			zoom.play();
        	}
        
    		public function removeQuesPanel()
        	{
        	
        	}
        
       
        public function evtListner_showNextAndWriteToXml(evt:MouseEvent):void
        {
        	if(questionStatement.text==""||AOption.text==""||AOption.text==""||AOption.text==""||AOption.text==""||askForAnswerText.text==""||askForWeightage.text=="")
        	{
        		Alert.show("Some of your entries are incomplete");
        		return;
        	}
        	if(askForAnswerText.text=="a"||askForAnswerText.text=="A"||askForAnswerText.text=="b"||askForAnswerText.text=="B"||askForAnswerText.text=="c"||askForAnswerText.text=="C"||askForAnswerText.text=="d"||askForAnswerText.text=="D")
        	{
        		// call to controller and consequently the model to save the questions as XML file
        		controller.test();
        	}
        	else
        	{
        		Alert.show("Enter the corrct answer");
        		return;
        	}
        	
        	assesmentPanel.removeChild(previousPanel);
        	questionNumber++;
        	previousRadioButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        	
        }
        
        public function evtListner_WriteToArray(evt:MouseEvent):void
		{
			//call to controller to save it in the array
			controller.saveMultipleChoiceQuestions();
			
		}
		
		private function evtListner_showPreviousQuestion(evt:MouseEvent):void
		{
			assesmentPanel.removeChild(QuesPanel);
			assesmentPanel.addChild(previousPanel);
			zoomInFunction(previousPanel);
			controller.showPreviousQuestion();
			
		}
        public function removeCorrectAnswer()
        {
        	
        }
   //--------------------------communication functions------------------------------------------------------//
   override public function update(model:Observable, infoObj_object:Object):void
		{
			var message:String=infoObj_object.message.toString();
			
			switch(message)
			{
				case "mouseUpEvent":
					trace("AssesmentView------->event:mouseup  infoObj.event :"+infoObj_object.event);
					super.recieveMouseUpEvent(infoObj_object.event);
					break;
			}
		}
   //------------------------------------------------------------------------------------------------------//     
	}
}